from manim import *

class CustomCurve8(VMobject):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        # 定义自定义曲线的控制点
        points = [
            [-35, -15, 0],
            [-35,-12,0],
            [-40,-5,0]
        ]
        self.set_points_smoothly(points)



class CustomCurve9(VMobject):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        # 定义自定义曲线的控制点
        points = [
            [-40, -15, 0],
            [-35.5,-10.5,0],
        ]
        self.set_points_as_corners(points)
class CustomCurve10(VMobject):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        # 定义自定义曲线的控制点
        points = [
            [-35.5,-10.5,0],
            [-37.5,-8,0],
            [-40,-5,0]
        ]
        self.set_points_smoothly(points)
class CustomCurve11(VMobject):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        # 定义自定义曲线的控制点
        points = [
            [-40,-5,0],
            
            [-40,-15,0]
        ]
        self.set_points_smoothly(points)


class math1(MovingCameraScene):
    def construct(self):
        self.camera.background_color = BLACK  # 设置背景颜色
        self.camera.frame_width = 100  # 设置边框宽度
        self.camera.frame_height = 60  # 设置边框高度
        self.camera.pixel_height = 1080  # 设置像素高度
        self.camera.pixel_width = 1920  # 设置像素宽度
        self.camera.center = ORIGIN  # 设置中心点位置
        self.camera.scale_factor = 1.0  # 设置缩放因子
        #设置横线
        for i in range(6*2+1):       
            dot1=Dot([-50,5*(i-6),0]).set_opacity(0.5)
            dot2=Dot([50,5*(i-6),0]).set_opacity(0.5)
            if i==6:
                line1=Line(dot1,dot2).set_color(WHITE).set_opacity(0.5)
                
            else:
                line1=Line(dot1,dot2).set_color(WHITE).set_opacity(0.5)
               
            self.add(dot1,dot2,line1)
        #设置竖线
        for i in range(10*2+1):        
            dot3=Dot([(i-10)*5,-30,0]).set_opacity(0.5)
            dot4=Dot([(i-10)*5,30,0]).set_opacity(0.5)
            if i==10:
                line2=Line(dot3,dot4).set_color(WHITE).set_opacity(0.5)
                
            else:
                line2=Line(dot3,dot4).set_color(WHITE).set_opacity(0.5)
            self.add(dot3,dot4,line2)
        #设置三个点
        dot1 = Dot(radius=1, color=RED)  
        dot1.move_to([-47.5,27.5,0]) 
        dot2 = Dot(radius=1, color=YELLOW)  
        dot2.move_to([-42.5,27.5,0])  
        dot3 = Dot(radius=1, color=GREEN)  
        dot3.move_to([-37.5,27.5,0])
        #镜头跟进效果 
        self.camera.frame.save_state()
        #题目出现
        text0 = Tex(r"\text{(张4)设平面区域}$D=\{  \left( x,y\right) | 2x^{2}+y^{2}\leq 2\sqrt{x^{2}+y^{2}},y\geq x\geq  0 \}  $"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,24,0])
        text1 = Tex(r"\text{,若}$\iint _{D}\dfrac{f\left( x,y\right) }{\sqrt{1-x^{2}}}d\sigma =a >0,f\left( x,y\right) $\text{是}$D$\text{上的连续函数}"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text0,DOWN,buff=1)
       
        text2 = Tex(
                    r"\text{(1)计算}$\iint _{D}\dfrac{1}{\sqrt{1-x^{2}}}d\sigma $"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-30,9,0])
        
        text3 = Tex(
                   r"\text{(2)证明:存在}$\left( \xi ,\eta\right) \in D$\text{,使得}$\left| f\left( \xi ,\eta\right) \right| \geq \dfrac{\sqrt{2}}{\pi }a$"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text2,RIGHT,buff=3)
        

        self.play(Write(text0),Write(text1),Write(text2),Write(text3))

        #镜头拉回
          #分析文本
        #text100=MarkupText("<b><i>分析/解：</i></b>",color=PINK).scale(5.0).move_to([-38,12.5,0])
        self.play(FadeIn(dot1),run_time=0.1)
        self.play(FadeIn(dot2),run_time=0.1)
        self.play(FadeIn(dot3),run_time=0.1)
        


        # 创建坐标系
        axes = Axes(
            x_range=[-3, 10, 3],  # 设置 x 轴范围，从 -π 到 π，步长为 π
            y_range=[-3, 10, 3],  # 设置 y 轴范围，从 -1.5 到 1.5，步长为 0.5
            axis_config={"color": WHITE, "stroke_width": 30},  # 设置坐标轴的颜色和线宽
            x_length=20,  # 控制 x 轴长度
            y_length=20,  # 控制 y 轴长度
        )
        axes.move_to([-35, -10, 0])  # 移动坐标系的位置

        # 绘制 1/x 函数图像
        # 分为两部分以避开 x=0
        #graph_left = axes.plot(lambda x: 5/x, color=BLUE, stroke_width=30, x_range=[-5, -0.1])
        #graph_right = axes.plot(lambda x: 5/x, color=PURE_RED, stroke_width=30, x_range=[0.1, 10])
        
       

        title = Tex(
                    r"$f\left( x\right) $",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-35,14,0])

       


        #self.play(Create(arrow10),Create(text15))
        # 将所有元素添加到场景中
        #self.play(Create(axes), Create(labels), Create(graph2))
         # 显示图像
        
        
        self.play(Create(axes))

        dot1=Dot([-40,-15,0])
        dot2=Dot([-25,0,0])
        line4=Line(dot1,dot2).set_color(WHITE)
        line4.set_stroke(width=30)  # 设置宽度为 30
        self.play(Create(line4))
        text9 = Tex(
                   r"\text{令}$\begin{cases}x=r\cos \theta \\y=r\sin \theta \end{cases}$",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([5,0,0])
        self.play(Write(text9))

        text10 = Tex(
                   r"$\Rightarrow r=\dfrac{2}{1+\cos ^{2}\theta }$",color=RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([30,0,0])
        self.play(Write(text10))

        text11 = Tex(
                   r"$\theta =0\Rightarrow r=1$",color=BLUE
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([30,-10,0])
        self.play(Write(text11))
        dot5 = Dot([-35, -15, 0], color=RED, radius=0.5)
        self.play(Create(dot5))
        text12 = Tex(
                   r"$\theta =\dfrac{\pi }{2}\Rightarrow r=2$",color=BLUE
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([30,-20,0])
        self.play(Write(text12))
        dot6 = Dot([-40, -5, 0], color=RED, radius=0.5)
        self.play(Create(dot6))
        curve8 = CustomCurve8(color=WHITE, stroke_width=20)
        curve9 = CustomCurve9(color=PURE_RED, stroke_width=60)
        curve10 = CustomCurve10(color=PURE_RED, stroke_width=60)
        curve11 = CustomCurve10(color=PURE_RED, stroke_width=60)
        
        
        self.play(  Create(curve8))
        self.play(  Create(curve9),Create(curve10),Create(curve11),FadeOut(text11),FadeOut(text12))
        self.wait()

        text1300 = Tex(
                   r"$\iint _{D}\dfrac{1}{\sqrt{1-x^{2}}}d\sigma =\int ^{\dfrac{\pi }{2}}_{\dfrac{\pi }{4}}d\theta \int _{0}^{\dfrac{2}{2+\cos ^{2}\theta }}\dfrac{r}{\sqrt{1-r^{2}\cos ^{2}\theta }}dr$",color=BLUE
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([15,-10,0])
        self.play(Write(text1300))

        self.play(text1300.animate().scale(1).move_to([15,0,0]),FadeOut(text10),FadeOut(text9)) 
        dot1=Dot([15,-5,0])
        dot2=Dot([40,-5,0])
        line3=Line(dot1,dot2).set_color(PURPLE)
        line3.set_stroke(width=30)  # 设置宽度为 30
        self.play(Create(line3))

        text13 = Tex(
                   r"$\int _\dfrac{\pi }{4}^{\dfrac{\pi }{2}}\left( -\dfrac{1}{\cos ^{2}\theta }\right) d\theta \int ^{\dfrac{2}{1+\cos ^{2}\theta }}_{0}\dfrac{\frac{1}{2}}{\sqrt{1-r^{2}\cos ^{2}}\theta }d1-r^{2}\cos ^{2}\theta  $",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([15,-13,0])
        self.play(Write(text13))
        dot1=Dot([0,-18,0])
        dot2=Dot([48,-18,0])
        line1=Line(dot1,dot2).set_color(PURPLE)
        line1.set_stroke(width=30)  # 设置宽度为 30
        self.play(Create(line1))

        text130 = Tex(
                   r"$ \sqrt{1-r^{2}\cos ^{2}\theta }| _{0}^{\dfrac{2}{1+\cos ^{2}\theta }} $",color=RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([15,-23,0])
        self.play(Write(text130))

        text14 = Tex(
                   r"$ \sqrt{1-\dfrac{4\cos ^{2}\theta }{\left( 1+\cos ^{2}\theta \right) ^{2}}}-1$",color=PURPLE
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([15,-25,0])
        self.play(
            ReplacementTransform(text130,text14)
        )
        self.wait(0.2)

        text15 = Tex(
                   r"$\sqrt{\dfrac{\left( 2-\cos ^{2}\theta \right) ^{2}}{\left( 1+\cos ^{2}\theta \right) ^{2}}}-1$",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([15,-25,0])
        self.play(
            ReplacementTransform(text14,text15)
        )
        self.wait(0.2)
        
        text16 = Tex(
                   r"$\dfrac{-2\cos ^{2}\theta }{1+\cos ^{2}\theta }$",color=BLUE
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([15,-25,0])
        self.play(
            ReplacementTransform(text15,text16)
        )
        self.wait(0.2)
        text17 = Tex(
                   r"$2\int _{\dfrac{\pi }{4}}^{\dfrac{\pi }{2}}\dfrac{1}{1+\cos ^{2}\theta }d\theta $",color=RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([10,-13,0])
        
        self.play(
            ReplacementTransform(text16,text17),
            ReplacementTransform(text13,text17),
            ReplacementTransform(line1,text17),
        )
        self.wait(0.2)

         #箭头引出
        arrow = Arrow(start=[-5,-20,0],end=[0,-17,0], color=WHITE
                       , stroke_width=20, tip_length=1)
        self.play(Create(arrow))
        #文本
        long_text=("特征识别:\n"
                     "伪奇偶性")
        text100 = Text(long_text,color=YELLOW,
                   font_size=200).move_to([-15,-20,0])
        
        self.play(Create(text100))
        text18 = Tex(
                   r"\text{分子分母同除}$\cos ^{2}x$",color=BLUE
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([15,-23,0])
        self.play(Write(text18))

        text19 = Tex(
                   r"$=2\int ^{\dfrac{\pi }{2}}_{\dfrac{\pi }{4}}\dfrac{1}{\tan ^{2}x+2}d\tan x$",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text17,RIGHT,buff=1)
        self.play(
            ReplacementTransform(text18,text19),
            
        )
        self.wait(0.2)
        text20 = Tex(
                   r"$=\sqrt{2}\cdot \left( \dfrac{\pi }{2}-\arctan \dfrac{\pi }{2}\right) $",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text19,DOWN,buff=3)
        self.play(Write(text20))

        self.play(Uncreate(text20),Uncreate(text19),Uncreate(text100),Uncreate(text17),Uncreate(text1300)
             ,Uncreate(line2),Uncreate(axes),Uncreate(curve8),Uncreate(curve9),Uncreate(curve10),Uncreate(curve11)
             ,Uncreate(dot5),Uncreate(dot6),Uncreate(line3),Uncreate(line4),Uncreate(arrow))
        

        rectangle=Rectangle(color=BLUE,fill_opacity=0.0,width=20,height=10
                            ,stroke_color=BLUE,stroke_width=20)
        rectangle.move_to([30, 8, 0])
        self.play(Create(rectangle))
        self.play(FadeOut(rectangle))
        rectangle1=Rectangle(color=BLUE,fill_opacity=0.0,width=20,height=10
                            ,stroke_color=BLUE,stroke_width=20)
        rectangle1.move_to([-15, 18, 0])
        self.play(Create(rectangle1))
        text21 = Tex(
                   r"$a=\iint _{D}\dfrac{f\left( x,y\right) }{\sqrt{1-x^{2}}}d\sigma $",color=RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-30,0,0])
        
        self.play(
            ReplacementTransform(rectangle1,text21)
        )
        self.wait(0.2)
        rectangle=Rectangle(color=BLUE,fill_opacity=0.0,width=20,height=10
                            ,stroke_color=BLUE,stroke_width=20)
        rectangle.move_to([30, 8, 0])
        self.play(Create(rectangle))
        self.play(FadeOut(rectangle))
        text22 = Tex(
                   r"$a=\left| \iint _{D}\dfrac{f\left( x,y\right) }{\sqrt{1-x^{2}}}d\sigma \right| $",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-30,0,0])
        self.play(
            ReplacementTransform(text21,text22)
        )
        self.wait(0.2)
        rectangle=Rectangle(color=BLUE,fill_opacity=0.0,width=20,height=10
                            ,stroke_color=BLUE,stroke_width=20)
        rectangle.move_to([30, 8, 0])
        self.play(Create(rectangle))
        self.play(FadeOut(rectangle))
        text23 = Tex(
                   r"$\leq \iint _{D}\left| \dfrac{f\left( x,y\right) }{\sqrt{1-x^{2}}}\right| d\sigma $",color=RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text22,RIGHT,buff=1)
        self.play(Create(text23))
        rectangle=Rectangle(color=BLUE,fill_opacity=0.0,width=20,height=10
                            ,stroke_color=BLUE,stroke_width=20)
        rectangle.move_to([-25, 8, 0])
        self.play(Create(rectangle))
        self.play(FadeOut(rectangle))
        text24 = Tex(
                   r"$\leq \left| f\left( \xi ,\theta\right) \right| \iint _{D}\dfrac{1}{\sqrt{1-x^{2}}}d\sigma $",color=PURPLE
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text23,RIGHT,buff=1)
        self.play(Create(text24))
        #下划线
        #下划线
        dot1=Dot([20,-5,0])
        dot2=Dot([35,-5,0])
        line=Line(dot1,dot2).set_color(WHITE)
        line.set_stroke(width=30)  # 设置宽度为 30
        self.play(Create(line))
        text25 = Tex(
                   r"$=\dfrac{\sqrt{2}}{2}\left( \pi -\arctan \dfrac{\sqrt{2}}{2}\right) \leq  \dfrac{\sqrt{2}}{2}\pi  $",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([28,-10,0])
        self.play(Create(text25))
        self.wait(0.2)
        text26 = Tex(
                   r"$\leq  \dfrac{\sqrt{2}}{2}\pi \left| f\left( \xi ,\theta\right) \right| $",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text24,RIGHT,buff=1)
        self.play(
            ReplacementTransform(text25,text26)
            ,self.camera.frame.animate.scale(1.1).move_to([0,0,0])
        )
        self.wait(0.2)
        

        text27 = Tex(
                   r"$\Rightarrow \left| f\left( \xi ,\theta\right) \right| \geq \dfrac{\sqrt{2}}{\pi }a$",color=PURE_RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(10.0).move_to([0,-15,0])
        self.play(Create(text27))
        self.wait(2)